package tingche;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import org.junit.jupiter.api.Test;

import tingche.bean.CarBean;
import tingche.bean.CarTingBean;
import tingche.bean.ShouFeiBean;
import tingche.dao.CarDao;
import tingche.dao.CarTingDao;
import tingche.dao.ShouFeiDao;

public class tingche {

	public static void main(String[] args) {
		while (true) {
			System.out.println("0.建停车场车位");
			// 登记待进车位车辆信息
			System.out.println("1.车辆登记");
			System.out.println("2.车辆入位");
			// 停车位状态 车牌、开始时间、结束时间、产生费用(计算)
			System.out.println("3.查询所有停车位的状态");
			// 按车牌及停车位号取车，取车时收停车费
			System.out.println("4.按车牌以及停车位号取车");
			// 查询统计全部的收费记录(车牌、开始时间、结束时间、产生的费用)
			System.out.println("5.查询统计全部的收费记录");
			tingche t = new tingche();
			t.caozuo();
		}
	}

	public void caozuo() {
		Scanner sc = new Scanner(System.in);
		String caozuo = sc.nextLine();
		switch (caozuo) {
		case "0":
			cheWei();
			break;
		case "1":
			dengji();
			break;
		case "2":
			jinChe();
			break;
		case "3":
			lookTingChe();
			break;
		case "4":
			selectByPaiTing();
			break;
		case "5":
			selectMoney();
			break;

		default:
			break;
		}
	}

	@Test
	public void dengji() {
		Scanner sc = new Scanner(System.in);
		System.out.println("登记车辆车牌号：");
		String chepai = sc.nextLine();
		System.out.println("登记车辆高度：");
		String h = sc.nextLine();
		BigDecimal height = new BigDecimal(h);
		CarBean car = new CarBean();

		car.setChepai(chepai);
		car.setHeight(height);

		CarDao cardao = new CarDao();
		int i = cardao.insert(car);
		System.out.println("成功登记" + i + "条记录");
	}

	@Test
	public void cheWei() {
		CarTingDao dao = new CarTingDao();
		System.out.println("输入有多少车位");
		Scanner sc = new Scanner(System.in);
		Integer n = Integer.parseInt(sc.nextLine());
		for (int i = 0; i < n; i++) {
			CarTingBean carTing = new CarTingBean();
			carTing.setId(i);
			carTing.setStat(0);
			dao.insert(carTing);
		}
		System.out.println("安装" + n + "个车位");
	}

	@Test
	public void jinChe() {

		Scanner sc = new Scanner(System.in);
		System.out.println("选择停车位号");
		Integer tingchewei = Integer.parseInt(sc.nextLine());

		System.out.println("扫描车牌号");
		String chepaihao = sc.nextLine();

		// Calendar获取开始时间
		Calendar start = Calendar.getInstance();
		Date time = start.getTime();

		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String timefmt = fmt.format(time);

		CarBean car = new CarBean();
		car.setChepai(chepaihao);
		// 根据车牌获得车信息高度
		CarDao cardao = new CarDao();
		CarBean car1 = cardao.selectByPai(car);
		int height = car1.getHeight().intValue();

		// 根据停车位号获取停车位信息
		CarTingBean ting = new CarTingBean();
		ting.setId(tingchewei);
		CarTingDao tingdao = new CarTingDao();
		CarTingBean ting1 = tingdao.selectByHao(ting);

		System.out.println(ting1.toString());
		// 获取当前停车位状态
		int statu = ting1.getStat();

//		for (int i = 0; i < carTingList.size(); i++) {
//			carting = carTingList.get(i);
//			if (carting.getId() == tingchewei) {
//				statu = carting.getStat();
//				break;
//			}
//		}
		// 1—高度不符合 2-停车位已占用
		if (statu == 1) {
			System.out.println("车位已占用");
			return;
		}
		if (height > 3) {
			System.out.println("车辆高度不符合要求");
			return;
		}
		if (statu == 0 && height < 3) {
			System.out.println("车辆入场");
		}

		// 需要carTing类状态修改
		ting.setStat(1);
		tingdao.updatestat(ting);

		ShouFeiBean shoufei = new ShouFeiBean();
		shoufei.setCheWeiHao(tingchewei);
		shoufei.setChePai(chepaihao);
		shoufei.setStartTime(timefmt);

		// 收费记录Dao
		ShouFeiDao sfdao = new ShouFeiDao();
		sfdao.insert(shoufei);

	}

	@Test
	public void lookTingChe() {

		CarTingDao tingdao = new CarTingDao();
		List<CarTingBean> list = tingdao.selectAll();
		for (int i = 0; i < list.size(); i++) {
			CarTingBean ting = list.get(i);
			System.out.println(ting.toString());
		}
	}

	@Test
	public void selectByPaiTing() {

		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的车牌");
		String chepai = sc.nextLine();
		System.out.println("请输入你的停车位号");
		Integer tingNum = Integer.parseInt(sc.nextLine());

		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		// 结束时间
		Calendar calend = Calendar.getInstance();
		long endTime = calend.getTimeInMillis();
		String end = fmt.format(calend.getTime());

		// 根据车牌和停车位号查询记录
		ShouFeiBean sfbean = new ShouFeiBean();
		sfbean.setChePai(chepai);
		sfbean.setCheWeiHao(tingNum);

		ShouFeiDao sfdao = new ShouFeiDao();
		ShouFeiBean jilu = sfdao.selectByPaiTing(sfbean);
		if (jilu == null) {
			System.out.println("车牌或停车位号不正确");
			return;
		}
		// 开始时间
		String sta = jilu.getStartTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		Date stadate = null;
		try {
			stadate = sdf.parse(sta);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long starttime = stadate.getTime();

		long hour = (endTime - starttime) / 1000 / 60 / 60;
//		System.out.println(endTime);
//		System.out.println(starttime);
//		System.out.println("————————————");
//		System.out.println(hour);
		int round = Math.round(hour);

		// 测试的时候间隔时间太短,也需要按一小时算
		if (round == 0) {
			round = 1;
		}
		BigDecimal Money = new BigDecimal(round * 2);
		jilu.setMoney(Money);
		jilu.setEndTime(end);
		sfdao.update(jilu);
		System.out.println("收取费用：" + Money);
		
		CarTingBean ting=new CarTingBean();
		ting.setId(tingNum);
		ting.setStat(0);
		
		CarTingDao tingdao=new CarTingDao();
		int i=tingdao.updatestat(ting);
		System.out.println("停车位更新"+i+"条数据");

	}

	@Test
	public void selectMoney() {
		System.out.println("全部收费记录：");
		ShouFeiDao dao=new ShouFeiDao();
		List<ShouFeiBean> list = dao.selectAll();
		for (int i = 0; i < list.size(); i++) {
			ShouFeiBean shouFeiBean = list.get(i);
			System.out.println(shouFeiBean.toString());
		}
	}
}
